package com.xingdata.jjxc.m.login.avt;

import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.xingdata.jjxc.App;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.base.Base1Activity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.http.HttpResponseListener;
import com.xingdata.jjxc.utils.ActivityTools;
import com.xingdata.jjxc.utils.Code;
import com.xingdata.jjxc.utils.MessageCode;
import com.xingdata.jjxc.utils.SystemInfo;

public class FindPwd1 extends Base1Activity implements OnClickListener {
	final String TAG = "FindPwd1";

//	private EditText findpwd_phone, findpwd_code;
	/** 旧密码 */
	private EditText old_pwd;
	/** 设置新密码 */
	private EditText new_pwd;
	/** 确认新密码 */
	private EditText confirm_new_pwd;
	private TextView findpwd_code_tv;
	private Button findpwd_btn;

	private String phoneNum = "";
	private Handler mHandler;
	private int time = 180;
	private Timer timer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// ExitApplication.getInstance().addActivity(this);
		// user = SP.getUserInfo(this);
		ActivityTools.addActivities(this);
		// 获取Http工具类
		// mAbHttpUtil = AbHttpUtil.getInstance(this);
		// mAbHttpUtil.setDebug(true);
		// mAbHttpUtil.setTimeout(15000);

		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			// 自动获取并填写手机号码
			phoneNum = bundle.getString("pNum");
//			findpwd_phone.setText(phoneNum);
		}

	}

	@Override
	protected int getContentViewId() {
		return R.layout.activity_find_pwd1;
	}

	@Override
	protected String getTitleText() {
		return "修改密码";
	}

	@Override
	protected void initViews() {
		old_pwd = (EditText) findViewById(R.id.old_pwd);
		new_pwd = (EditText) findViewById(R.id.new_pwd);
		confirm_new_pwd = (EditText) findViewById(R.id.confirm_new_pwd);
		
//		findpwd_phone = (EditText) findViewById(R.id.findpwd_phone_et);
//		findpwd_code = (EditText) findViewById(R.id.findpwd_code_et);
//		findpwd_code_tv = (TextView) findViewById(R.id.findpwd_code_tv);
		findpwd_btn = (Button) findViewById(R.id.findpwd_btn);
		findpwd_btn.setOnClickListener(this);
//		findpwd_code_tv.setOnClickListener(this);
		mHandler = new Handler() {
			public void handleMessage(android.os.Message msg) {
				switch (msg.what) {
				case MessageCode.MessageCode_RefreshControl:
					timerDown();
					break;

				case MessageCode.MessageCode_ShowToast:
					showToast(msg.getData().getString("result"));
					break;

				default:
					break;
				}
			};
		};
	}

	private void timerDown() {
		timer = new Timer();
		timer.schedule(new MyTimerTask(), 1000, 1000);
		findpwd_code_tv.setEnabled(false);
		// TODO 发送短信到指定手机
	}

	// 定时器
	class MyTimerTask extends TimerTask {
		@Override
		public void run() {
			runOnUiThread(new Runnable() { // UI thread
				@Override
				public void run() {
					if (time == 0) {
						time = 60;
						findpwd_code_tv.setEnabled(true);
						findpwd_code_tv.setText("重新发送");
						timer.cancel();
					} else {
						time--;
						findpwd_code_tv.setText(time + "秒后重发");
					}
				}
			});
		}

	}

	@Override
	public void onClick(View v) {
//		String phone = findpwd_phone.getText().toString();

		switch (v.getId()) {
		case R.id.findpwd_btn:
			String oldpwd = old_pwd.getText().toString();
			String newpwd = new_pwd.getText().toString();
			String confirmnewpws = confirm_new_pwd.getText().toString();
			if (newpwd.length()<6) {
				showToast(R.string.error_pwd_length);
			}else if(!newpwd.equals(confirmnewpws)){
				showToast(R.string.error_pwd_not_same);
				confirm_new_pwd.setText("");
			}else {
				doPost_modifyPwd(oldpwd, confirmnewpws);
			}
			
			
//			String code = findpwd_code.getText().toString();
//			if (code.length() == 0) {
//				showToast("请输入验证码");
//			} else {
//				doPost_reset(phone, code);
//			}

			break;

//		case R.id.findpwd_code_tv:
//			if (phone.equals("") || (phone.length() != 11)) {
//				showToast(R.string.error_phone_num);
//			} else {
//				doPost_sendCode(phone);
//			}
//			break;
		default:
			break;
		}
	}
	
	/**
	 * 修改密码
	 */
	private void doPost_modifyPwd(String oldpwd, String newpwd){
		if (isNetworkConnected() == false) {
			showToast("网络连接断开，请检查网络");
			return;
		}
		paramsMap = new HashMap<String, String>();
		paramsMap.put("userid", SystemInfo.getUserInfo().getUserid());
		paramsMap.put("access_token", SystemInfo.getUserInfo().getToken());
		paramsMap.put("type", "password");
		paramsMap.put("oldpwd", oldpwd);
		paramsMap.put("newpwd", newpwd);
		mHttpUtil.Post(App.ZZD_REQUEST_UPDATEUSERPWD, paramsMap, new HttpResponseListener() {
			
			@Override
			public void onSuccess(RespEntity respEntity) {
				// respEntity.getMsg() = "密码修改成功"
				showToast(respEntity.getMsg());
				Log.d(TAG, "onSuccess="+respEntity);
				Log.d(TAG, "onSuccess="+respEntity.getMsg());
				Log.d(TAG, "onSuccess="+respEntity.getState());
			}
			
			@Override
			public void onLoading(int vurrent) {
				// TODO Auto-generated method stub
				Log.d(TAG, "onLoading="+vurrent);
			}
			
			@Override
			public void onFailure(String result) {
				// result = "原始密码错误"、“缺少原始密码、新密码”
				showToast(result);
				Log.d(TAG, "onFailure ="+result);
			}
		});
	}

	/**
	 * 获取验证码
	 * @param mobile
	 */
	private void doPost_sendCode(String mobile) {
		if (isNetworkConnected() == false) {
			showToast("网络连接断开，请检查网络");
			return;
		}
		String phpwd = mobile.substring(7, 11) + mobile.substring(0, 7);
		String findkeyword = Code.CodeMD5(Code.CodeMD5(phpwd).toLowerCase())
				.toLowerCase();
		paramsMap = new HashMap<String, String>();
		paramsMap.put("mobile", mobile);
		paramsMap.put("keyword", findkeyword);
		paramsMap.put("checktype", "editpwd");

		mHttpUtil.Post(App.ZZD_REQUEST_SENDCODE, paramsMap,
				new HttpResponseListener() {

					@Override
					public void onSuccess(RespEntity respEntity) {
						Message message = new Message();
						message.what = MessageCode.MessageCode_RefreshControl;
						mHandler.sendMessage(message);
					}

					@Override
					public void onFailure(String result) {
						Bundle bundle = new Bundle();
						bundle.putString("result", result);
						Message message = new Message();
						message.setData(bundle);
						message.what = MessageCode.MessageCode_ShowToast;
						mHandler.sendMessage(message);
					}

					@Override
					public void onLoading(int vurrent) {
						// TODO Auto-generated method stub
						
					}
				});
	}

	/**
	 * 发送验证码,确认修改
	 * @param mobile
	 * @param code
	 */
	private void doPost_reset(final String mobile, final String code) {
		if (isNetworkConnected() == false) {
			showToast("网络连接断开，请检查网络");
			return;
		}
		paramsMap = new HashMap<String, String>();
		paramsMap.put("mobile", mobile);
		paramsMap.put("checknum", code);

		mHttpUtil.Post(App.ZZD_REQUEST_CHECKCODE, paramsMap,
				new HttpResponseListener() {

					@Override
					public void onSuccess(RespEntity respEntity) {
						Intent intent = new Intent(FindPwd1.this,
								FindPwd2.class);
						Bundle bundle = new Bundle();
						bundle.putString("mobile", mobile);
						bundle.putString("checknum", code);
						intent.putExtras(bundle);
						ActivityTools.startActivity(FindPwd1.this,intent);
						finish();
					}

					@Override
					public void onFailure(String result) {
						Bundle bundle = new Bundle();
						bundle.putString("result", result);
						Message message = new Message();
						message.setData(bundle);
						message.what = MessageCode.MessageCode_ShowToast;
						mHandler.sendMessage(message);
					}

					@Override
					public void onLoading(int vurrent) {
						// TODO Auto-generated method stub
						
					}
				});


	}

	class MyCountTimer extends CountDownTimer {
		public MyCountTimer(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onFinish() {
			findpwd_code_tv.setText("获取验证码");
			findpwd_code_tv.setClickable(true);
		}

		@Override
		public void onTick(long millisUntilFinished) {
			findpwd_code_tv.setText(String.valueOf(millisUntilFinished)
					+ "秒后重新获取");
			findpwd_code_tv.setClickable(false);
		}
	}

}
